{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T03:08:00.915546Z",
     "start_time": "2025-01-08T03:08:00.410031Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:08:02.941181Z",
     "start_time": "2025-01-08T03:08:02.934596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2.index)"
   ],
   "id": "160b9ef0889c7ba7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:10:30.268574Z",
     "start_time": "2025-01-08T03:10:29.993477Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 索引对象的值不可变（上面代码增加）\n",
    "# df_obj2.index[0] = 2"
   ],
   "id": "bbf8d72504634e0e",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Index does not support mutable operations",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[5], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m# 索引对象的值不可变（上面代码增加）\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m \u001B[43mdf_obj2\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mindex\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m2\u001B[39m\n",
      "File \u001B[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\pandas\\core\\indexes\\base.py:5371\u001B[0m, in \u001B[0;36mIndex.__setitem__\u001B[1;34m(self, key, value)\u001B[0m\n\u001B[0;32m   5369\u001B[0m \u001B[38;5;129m@final\u001B[39m\n\u001B[0;32m   5370\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__setitem__\u001B[39m(\u001B[38;5;28mself\u001B[39m, key, value) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m-> 5371\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIndex does not support mutable operations\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
      "\u001B[1;31mTypeError\u001B[0m: Index does not support mutable operations"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 常见的Index种类",
   "id": "8cceaf028c87b7e5"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# •Index，索引  可以是各种类型\n",
    "# •Int64Index，整数索引\n",
    "# •MultiIndex，层级索引，难度较大\n",
    "# •DatetimeIndex，时间戳类型"
   ],
   "id": "4c9e1419826cec"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# serise索引",
   "id": "5ba769f2bd74ed7b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:12:43.794668Z",
     "start_time": "2025-01-08T03:12:43.784494Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5), index = list(\"abcde\"))\n",
    "print(ser_obj)\n",
    "ser_obj.index"
   ],
   "id": "ca1e70f23297aa0f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:13:30.887263Z",
     "start_time": "2025-01-08T03:13:30.881864Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名，可以用索引位置或来取\n",
    "print(ser_obj['b']) #索引名\n",
    "print(ser_obj[2]) #位置索引"
   ],
   "id": "a25c54d234376553",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\24442\\AppData\\Local\\Temp\\ipykernel_74148\\2834633063.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[2]) #位置索引\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:16:25.738236Z",
     "start_time": "2025-01-08T03:16:25.734039Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#这种取索引的方法比较规范\n",
    "print(ser_obj.loc['b']) #索引名\n",
    "print(ser_obj.iloc[2]) #位置索引"
   ],
   "id": "c269544d5b6217ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:18:39.717015Z",
     "start_time": "2025-01-08T03:18:39.710844Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #记住索引名  左闭右闭"
   ],
   "id": "c30830196c00ce43",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:19:41.512087Z",
     "start_time": "2025-01-08T03:19:41.502867Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "id": "2a443d8d0836c631",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:20:05.808335Z",
     "start_time": "2025-01-08T03:20:05.802793Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool)"
   ],
   "id": "3768b7c4369453db",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T03:25:22.283439Z",
     "start_time": "2025-01-08T03:25:22.277561Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "print(ser_obj[ser_bool])#取出大于2的元素\n",
    "\n",
    "print(ser_obj[ser_obj > 2]) #取出大于2的元素"
   ],
   "id": "ad4b6873e43dec1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFrame索引",
   "id": "6b2fe14b385e9422"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "DataFrame索引与serise的区别是可以是行索引、列索引、混合索引",
   "id": "764bfb7e1a70936a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T04:10:12.500985Z",
     "start_time": "2025-01-08T04:10:12.494430Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "id": "530ab7a512a67ea0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -0.910172  0.223482  0.597263  1.489658\n",
      "1 -0.428334  0.790389  0.045588  1.493024\n",
      "2 -2.184333  0.315379 -0.747875 -0.299582\n",
      "3  0.927456 -0.811722 -1.273603  0.079472\n",
      "4 -1.053358  0.459574  0.859033 -1.642873\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T04:11:41.381234Z",
     "start_time": "2025-01-08T04:11:41.373373Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a']) # 返回Series类型\n",
    "print('-'*50)\n",
    "print(df_obj[['a']]) # 返回DataFrame类型\n",
    "print('-'*50)\n",
    "print(type(df_obj[['a']])) # 返回DataFrame类型"
   ],
   "id": "9373f133a2efcfcd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.910172\n",
      "1   -0.428334\n",
      "2   -2.184333\n",
      "3    0.927456\n",
      "4   -1.053358\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0 -0.910172\n",
      "1 -0.428334\n",
      "2 -2.184333\n",
      "3  0.927456\n",
      "4 -1.053358\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T04:12:33.567123Z",
     "start_time": "2025-01-08T04:12:33.558106Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = list('abcd'),\n",
    "                      index=list('abcde'))\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a'])  #建议不用,拿的是列\n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])  #拿的是行\n",
    "print('-'*50)"
   ],
   "id": "871e58399165000b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.501543 -1.392454 -0.784200  0.292313\n",
      "b -1.115479 -1.593457  0.086405  1.705708\n",
      "c -0.813776  1.288260  0.670043  0.154602\n",
      "d -0.378769 -0.027998 -2.593026 -0.665207\n",
      "e -0.771777 -0.043308 -1.413866 -0.120631\n",
      "--------------------------------------------------\n",
      "a   -0.501543\n",
      "b   -1.115479\n",
      "c   -0.813776\n",
      "d   -0.378769\n",
      "e   -0.771777\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a   -0.501543\n",
      "b   -1.392454\n",
      "c   -0.784200\n",
      "d    0.292313\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T04:15:48.685868Z",
     "start_time": "2025-01-08T04:15:48.674355Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d']) #连续索引\n",
    "print(df_obj.loc[['a','c'], ['b','d']]) #不连续索引\n",
    "print(df_obj.loc[['c'],['b']]) #取一个值,返回的是DataFrame类型\n",
    "#方括号一般默认是要取多个值，所以就写一个，返回的也是DataFrame类型\n",
    "print(df_obj.loc['c','b'])  #取一个值"
   ],
   "id": "8d3c22d221ed04a7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a -1.392454 -0.784200  0.292313\n",
      "b -1.593457  0.086405  1.705708\n",
      "c  1.288260  0.670043  0.154602\n",
      "          b         d\n",
      "a -1.392454  0.292313\n",
      "c  1.288260  0.154602\n",
      "         b\n",
      "c  1.28826\n",
      "1.288260010693771\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:42:00.899879Z",
     "start_time": "2025-01-08T05:42:00.894466Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# iloc位置索ser_obj\n",
    "print('-'*50)\n",
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print('-'*50)\n",
    "print(ser_obj.iloc[1:3]) # 前闭后开[)，效率高引"
   ],
   "id": "1f47cead6999d1e9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:43:27.327741Z",
     "start_time": "2025-01-08T05:43:27.318669Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0:2, 0:2]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[[0,2], [0,2]]) # 不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0,0]) # 取一个值"
   ],
   "id": "d9f7ec91c345b2a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.501543 -1.392454 -0.784200  0.292313\n",
      "b -1.115479 -1.593457  0.086405  1.705708\n",
      "c -0.813776  1.288260  0.670043  0.154602\n",
      "d -0.378769 -0.027998 -2.593026 -0.665207\n",
      "e -0.771777 -0.043308 -1.413866 -0.120631\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "a -0.501543 -1.392454\n",
      "b -1.115479 -1.593457\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a -0.501543 -0.784200\n",
      "c -0.813776  0.670043\n",
      "--------------------------------------------------\n",
      "-0.5015431786645395\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:44:37.561005Z",
     "start_time": "2025-01-08T05:44:37.552549Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "#loc基于标签选择数据，左闭右闭，iloc基于位置选择数据，左闭右开，传入的参数第一个都是行，第二个都是列\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2]) #左闭右开 2行,不选具体的列，逗号省略了\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) #左闭右闭 3行"
   ],
   "id": "4148e7da5a39a015",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.109332  0.393976 -1.812405  1.520203\n",
      "1  0.345423 -0.937645  1.660306 -0.561485\n",
      "2 -1.205140  1.205088  0.665772 -0.032994\n",
      "3  0.130727  0.283001  1.483855  0.704493\n",
      "4 -0.323120 -1.626113 -0.662533 -0.446778\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.109332  0.393976 -1.812405  1.520203\n",
      "1  0.345423 -0.937645  1.660306 -0.561485\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.109332  0.393976 -1.812405  1.520203\n",
      "1  0.345423 -0.937645  1.660306 -0.561485\n",
      "2 -1.205140  1.205088  0.665772 -0.032994\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 对齐运算",
   "id": "51bbaec8ee15c09b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:54:58.145994Z",
     "start_time": "2025-01-08T05:54:58.136716Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy中，尺寸不对会进行广播或者直接不能运算，pandas中尺寸不对会变成nan\n",
    "s1 = pd.Series(range(10, 20), index = range(10))\n",
    "s2 = pd.Series(range(20, 25), index = range(5))\n",
    "# Series 对齐运算\n",
    "print('s1+s2: ')\n",
    "s3=s1+s2\n",
    "print(s3)  #缺失数据默认是NaN  np.nan"
   ],
   "id": "f9034d7036297ba6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1+s2: \n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:57:56.610991Z",
     "start_time": "2025-01-08T05:57:56.605737Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#两个长度不同的一维ndarray相加,numpy中的广播机制\n",
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = np.array([1]) # 长度为1\n",
    "print(a2.shape)\n",
    "print(a2)\n",
    "print(a1+a2)"
   ],
   "id": "26eefed7375c4418",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "[1]\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T05:58:08.413343Z",
     "start_time": "2025-01-08T05:58:08.406159Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.isnan(s3[6]))\n",
    "print('-'*50)\n",
    "print(s2.add(s1, fill_value = 0))  #未对齐的数据将和填充值做运算\n",
    "print(s2.sub(s1, fill_value = 0))"
   ],
   "id": "5d70aca87e0eca5a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "--------------------------------------------------\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "0    10.0\n",
      "1    10.0\n",
      "2    10.0\n",
      "3    10.0\n",
      "4    10.0\n",
      "5   -15.0\n",
      "6   -16.0\n",
      "7   -17.0\n",
      "8   -18.0\n",
      "9   -19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T06:02:23.586159Z",
     "start_time": "2025-01-08T06:02:23.561732Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#df的对齐运算\n",
    "import numpy as np\n",
    "df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b'])\n",
    "df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-'*50)\n",
    "print(df2.dtypes)\n",
    "print(df1-df2)\n",
    "print(df2.sub(df1, fill_value = 2)) #未对齐的数据将和填充值做运算"
   ],
   "id": "4c90b5de04918e84",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "a    float64\n",
      "b    float64\n",
      "c    float64\n",
      "dtype: object\n",
      "     a    b   c\n",
      "0  0.0  0.0 NaN\n",
      "1  0.0  0.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "     a    b    c\n",
      "0  0.0  0.0 -1.0\n",
      "1  0.0  0.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总结：没对齐的元素，默认填充NaN，对齐运算时，fill_value参数可以指定填充值。",
   "id": "4e70cde5fe3b5c8a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "8ca6c7b6d947b36"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
